#include "EditorScriptIdentifiers.h"



std::weak_ptr<CEditorScriptNamespace> CEditorScriptNamespace::FindNamespace(const CString & InName)
{
    for(auto& InnerNamespace : InnerNamespaces)
    {
        if(InnerNamespace.expired())
        {
            continue;
        }

        auto SharedInnerNamespace=InnerNamespace.lock();

        if(SharedInnerNamespace->Name == InName)
        {
            return InnerNamespace;
        }
    }
    return std::weak_ptr<CEditorScriptNamespace>();
}

std::weak_ptr<CEditorScriptClass> CEditorScriptNamespace::FindClass(const CString & InName)
{
    for(auto& Class : Classes)
    {
        if(Class.expired())
        {
            continue;
        }

        auto SharedClass=Class.lock();

        if(SharedClass->Name == InName)
        {
            return Class;
        }
    }
    return std::weak_ptr<CEditorScriptClass>();
}

std::weak_ptr<CEditorScriptClass> CEditorScriptNamespace::FindClassRecursively(const CString &InName)
{
    auto FoundClass=FindClass(InName);

    if(!FoundClass.expired())
    {
        return FoundClass;
    }

    for(auto& InnerNamespace : InnerNamespaces)
    {
        if(InnerNamespace.expired())
        {
            continue;
        }

        auto SharedInnerNamespace=InnerNamespace.lock();

        auto FoundClass=SharedInnerNamespace->FindClassRecursively(InName);

        if(!FoundClass.expired())
        {
            return FoundClass;
        }
    }

    return std::weak_ptr<CEditorScriptClass>();
}

std::weak_ptr<CEditorScriptClass> CEditorScriptClass::FindClass(const CString &InName)
{
    for(auto& InnerClass : InnerClasses)
    {
        if(InnerClass.expired())
        {
            continue;
        }

        auto SharedInnerClass=InnerClass.lock();

        if(SharedInnerClass->Name == InName)
        {
            return InnerClass;
        }
    }
    return std::weak_ptr<CEditorScriptClass>();
}

std::weak_ptr<CEditorScriptField> CEditorScriptClass::FindField(const CString & InName)
{
    for(auto& Field : Fields)
    {
        if(Field.expired())
        {
            continue;
        }

        auto SharedField=Field.lock();

        if(SharedField->Name == InName)
        {
            return Field;
        }
    }
    return std::weak_ptr<CEditorScriptField>();
}

std::weak_ptr<CEditorScriptFunction> CEditorScriptClass::FindFunction(const CString &InName)
{
    for(auto& Function : Functions)
    {
        if(Function.expired())
        {
            continue;
        }

        auto SharedFunction=Function.lock();

        if(SharedFunction->Name == InName)
        {
            return Function;
        }
    }
    return std::weak_ptr<CEditorScriptFunction>();
}

std::weak_ptr<CEditorScriptVariable> CEditorScriptFunction::FindVariable(const CString &InName)
{
    for(auto& Variable : Variables)
    {
        if(Variable.expired())
        {
            continue;
        }

        auto SharedVariable=Variable.lock();

        if(SharedVariable->Name == InName)
        {
            return Variable;
        }
    }
    return std::weak_ptr<CEditorScriptVariable>();
}
